<?php
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\ArtCate as ArtCateModel;
use app\common\model\ArtCate;
use app\common\model\Article;
use think\Db;
use think\Exception;
use think\facade\Request;
use think\facade\Session;

class Index extends Base
{
    public function index()
    {
        $cateId = Request::param('cate_id');
        $map = [];
        $keywords = Request::param('keywords');
        if ($keywords){
            $map[] = ['title', 'like', '%'.$keywords.'%'];
        }
        if (isset($cateId)){
            $res = ArtCateModel::get($cateId);
            $map[] = ['cate_id', '=', $cateId];
            $this->view->assign('cateName', $res->name);
        }else{
            $this->view->assign('cateName', '全部文章');
        }
        $artList = Db::name('article')
            ->where($map)
            ->order('create_time', 'desc')
            ->paginate(2);
        $this->view->assign('artList', $artList);
        $this->view->assign('empty', '<h3>没有文章</h3>');
        return $this->view->fetch();
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function insert(){
        $this->isLogin();
        $this->assign('title','发布文章');
        $data = ArtCateModel::field('id,name')->select();
        $this->assign('catelist', $data);
        return $this->view->fetch();
    }

    public function save(){
        if ( ! Request::isPost()){
            return $this->error('请求类型错误', 'insert');
        }
        $data = Request::post();
        $res = $this->validate($data, 'app\common\validate\Article');
        if (true !== $res){
            return $this->error($res, 'insert');
        }
        try{
            $file = Request::file('title_img');
        }catch (Exception $e){
            return $this->error($e->getMessage(), 'insert');
        }
        $info = $file->validate([
            'size'=>'10240000',
            'ext'=>'jpeg,jpg,png,gif'
        ])->move('uploads/');
        if ($file->getError()){
            return $this->error($file->getError(), 'insert');
        }
        $data['title_img'] = $info->getSaveName();
        $data['user_id'] = Session::get('user_id');
        if (Article::create($data)){
            return $this->success('文章发布成功','index/index');
        }
        return $this->error('文章发布失败');
    }

    public function detail(){
        $art_id = Request::param('id');
        if (empty($art_id)){
            return $this->error('非法访问');
        }
        $art = Article::get(function ($query) use ($art_id){
            $query->field('id,user_id,cate_id,create_time,pv,content,title_img,title')->find($art_id)->setInc('pv',1);
        });
        $this->assign('art',$art);
        return $this->view->fetch();
    }

    public function fav(){
        if ( ! Request::isAjax()){
            return $this->error('非法访问');
        }
        if ( ! Session::has('user_id')){
            return ['status'=>0, 'msg'=>'请先登录'];
        }
        $artId = Request::param('artId');
        $user_id = Session::get('user_id');
        $condition = [
            ['art_id', '=', $artId],
            ['user_id', '=', $user_id]
        ];
        $has = Db::name('user_fav')->where($condition)->count();
        if ($has > 0){
            Db::name('user_fav')->where($condition)->delete();
            return ['status'=>1, 'msg'=>'已取消'];
        }else{
            Db::name('user_fav')->data(['user_id'=>$user_id, 'art_id'=>$artId])->insert();
            return ['status'=>2, 'msg'=>'收藏成功'];
        }
    }
}
